import MetaUtils from "@/utils/MetaUtils";
import Utils from "@/utils/basic.utils";
import {FuncMetas, SearchFuncMetas} from "@/components/OptionsMetaData";
import {EventBus, FunEventType} from "@/event";
import {foreignMetas, quoteMetas} from "@/metas/ComponentMetas";

const basicLabel = '基础配置';
const searchFormMetas = [
    {field: 'title', title: '字段标题', type: 'text'},
    {field: 'field', title: '表单字段', type: 'text', disabled: true}
]

const TextTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const SelectTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const RadioTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const RateTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const DateTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas
            , {title: '日期格式', field: 'format', type: 'text', default: 'yyyy-MM-DD'}
        ]
    },
}
const quoteTabMetas = {
    basic: {label: basicLabel, metas: [
            ...searchFormMetas,
        ]
    }
}
const SwitchTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const CheckboxTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const NumberTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const SliderTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const CascadeTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}

const StreeTabMetas = {
    basic: {label: basicLabel, metas: [...searchFormMetas]},
}
const QuoteTabMetas = {
    basic: {label: basicLabel, metas: [
            ...searchFormMetas,
            {title: '引用字段', field: 'quote', type: 'cascade', store: 'foreignList', disabled: true}
        ]
    },
}
const ForeignTabMetas = {
    basic: {label: basicLabel, metas: [
            ...searchFormMetas,
            {title: '外键', field: 'foreign', type: 'cascade', store: 'tableList', disabled: true}
        ]
    },
}
const SearchFormTabMetas = {
    TextTabMetas, NumberTabMetas, SelectTabMetas, QuoteTabMetas
    , RadioTabMetas, CascadeTabMetas, CheckboxTabMetas, StreeTabMetas
    , SliderTabMetas, SwitchTabMetas , DateTabMetas, RateTabMetas, ForeignTabMetas
}

function Text({id, type, field, title}) {
    return {id, type, field, title}
}
function Select({id, type, field, title}) {
    return {id, type, field, title}
}
function Radio({id, type, field, title}) {
    return {id, type, field, title}
}
function Rate({id, type, field, title}) {
    return {id, type, field, title}
}
function Date({id, type, field, title}) {
    return {id, type, field, title}
}
function Slider({id, type, field, title}) {
    return {id, type, field, title}
}
function Checkbox({id, type, field, title}) {
    return {id, type, field, title}
}
function Number({id, type, field, title}) {
    return {id, type, field, title}
}
function Password({id, type, field, title}) {
    return {id, type, field, title}
}
function Complete({id, type, field, title}) {
    return {id, type, field, title}
}
function Foreign({id, type, field, title, foreign}) {
    return {id, type, field, title, foreign}
}
function Quote({id, type, field, title, quote}) {
    return {id, type, field, title, quote}
}
function Switch({id, type, field, title}) {
    return {id, type, field, title}
}
function Mentions({id, type, field, title}) {
    return {id, type, field, title}
}
function Cascade({id, type, field, title}) {
    return {id, type, field, title}
}

const SearchFormTabModel = {Text, Select, Slider, Switch, Rate, Radio, Checkbox
    , Cascade, Complete, Date, Mentions, Foreign, Quote, Password, Number}

function getSearchFormTabMetas(type) {
    type = Utils.firstUpperCase(type) + 'TabMetas';
    let searchFormTabMeta = SearchFormTabMetas[type];
    if(null == searchFormTabMeta) {
        return console.warn(`搜索栏不支持此类型[${type}]`);
    }

    return MetaUtils.cloneTabMetas(searchFormTabMeta);
}

function getSearchFormTabModel(model) {
    let type = Utils.firstUpperCase(model.type);
    let searchFormTabModelFn = SearchFormTabModel[type];
    if(searchFormTabModelFn != null) {
        return searchFormTabModelFn(model);
    } else {
        return console.error(`找不到对应类型[${type}]的处理函数`)
    }
}

export const SearchMeta = {
    basic: {label: basicLabel, metas: [
            {field: 'func', title: '功能点', type: 'select', default: ['query', 'add', 'reset']
                , data: SearchFuncMetas, mode: 'multiple', change(value) {
                    EventBus.publish(FunEventType.addSearchFunc, value);
                }}
        ]
    },
}

export {getSearchFormTabMetas, getSearchFormTabModel}
